package com.base.map;

import android.app.Activity;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeAddress;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.amap.api.services.geocoder.StreetNumber;
import com.base.commonlib.base.BaseApp;
import com.base.commonlib.base.presenter.BasePresenter;
import com.base.commonlib.event.AddressEvent;
import com.base.commonlib.utils.RxBus2;

import java.util.ArrayList;
import java.util.List;

import static com.base.map.LocationUtil.LOCATION_SUCCESS;

/**
 * Created by arvin on 2018/10/28.
 */

public class MapPoiPresenter extends BasePresenter<MapPoiView> implements GeocodeSearch.OnGeocodeSearchListener {

    private LocationUtil mLocationUtil;
    private GeocodeSearch mGeocodeSearch;
    private String mStreet;
    public List<PoiItem> mPoiItems=new ArrayList<>();
    private double mLatitude;
    private double mLongitude;

    public MapPoiPresenter(MapPoiView view, Activity activity) {
        super(view, activity);
    }
    public void setALLude(double latitude, double longitude){
        mLatitude=latitude;
        mLongitude=longitude;
        mGeocodeSearch = new GeocodeSearch(BaseApp.getApp());
        mGeocodeSearch.setOnGeocodeSearchListener(this);
        mLocationUtil = new LocationUtil(BaseApp.getApp());
        mLocationUtil.setListener(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                mView.hidePb();
                mLocationUtil.stopLocation();
                if(aMapLocation!=null)
                {
                    if(aMapLocation.getErrorCode()==LOCATION_SUCCESS)
                    {
                        mView.addMarker(aMapLocation.getLatitude(),aMapLocation.getLongitude());
                    }else
                    {
                        mView.showMes("定位失败!");
                    }
                }else
                {
                    mView.showMes("定位失败!");
                }
            }
        });
    }


    @Override
    public void start() {
        super.start();
        if(mLongitude!=0&&mLongitude!=0)
        {
            mView.addMarker(mLatitude,mLongitude);
        }else
        {
            locate();
        }

    }


    public void locate() {
        mView.showPb();
        mLocationUtil.startLocation();
    }

    public void geocodeSearch(double latitude, double longitude) {
        LatLonPoint latLonPoint = new LatLonPoint(latitude, longitude);
        RegeocodeQuery regeocodeQuery = new RegeocodeQuery(latLonPoint, 500, GeocodeSearch.AMAP);
        mGeocodeSearch.getFromLocationAsyn(regeocodeQuery);
    }

    public void senAddress(PoiItem poiItem) {
        AddressEvent addressEvent = new AddressEvent();
        addressEvent.setStreet(mStreet);
//        addressEvent.setAddress(poiItem.getTitle()+"("+poiItem.getSnippet()+")");
        addressEvent.setAddress("("+poiItem.getTitle()+")"+poiItem.getSnippet());
        LatLonPoint latLonPoint = poiItem.getLatLonPoint();
        addressEvent.setLatitude(latLonPoint.getLatitude());
        addressEvent.setLongitude(latLonPoint.getLongitude());
        RxBus2.getInstance().post(addressEvent);
    }


    @Override
    public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
        Log.i("","");
        if(i==1000)
        {
            RegeocodeAddress regeocodeAddress = regeocodeResult.getRegeocodeAddress();
            StreetNumber streetNumber = regeocodeAddress.getStreetNumber();
            mStreet = streetNumber.getStreet();
            List<PoiItem> pois = regeocodeAddress.getPois();
            if(pois!=null)
            {
                mPoiItems.clear();
                mPoiItems.addAll(pois);
            }
            mView.setInfo();

        }


    }

    @Override
    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
        Log.i("","");
    }

    @Override
    public void release() {
        super.release();
        mLocationUtil.destroyLocation();

    }



}
